<?php

/**
 * @Author:      lyt
 * @DateTime:  2017-08-01 16:12:59
 * @Description: 用于订单对于套餐卡产品的使用
 */
class ScsOrderServiceCardController extends BaseController
{
    /**
     * @Author:      lyt
     * @DateTime:  2017-08-01 16:22:45
     * @Description: 用户套餐卡列表
     */
    public function serviceCardList(){
    	$validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'user_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
    	do{
    		if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $scd=ServiceCardDetail::where('user_id',Input::get('user_id'))->where('status',2)->where("end_date",">=",date("Y-m-d",time()))->get();//找到该用户的正常的能用的套餐卡
            $finally_arr=array();
            foreach ($scd as $_scd) {
                $scb=ServiceCardBase::find($_scd->service_card_base_id);
                $scbs=ServiceCardBaseShopUse::where("service_card_base_id",$scb->id)->where("shop_id",$sc_user->shop_id)->where("status",1)->first();//判断是否支持该门店
                if(is_null($scbs)||$scb->user_status==0){//改套餐卡不支持该门店 或者 该类型套餐卡停用
                    continue;
                }
                if($scb->limit_shop==1&&$_scd->shop_id!=0&&$_scd->shop_id!=$sc_user->shop_id){//说明套餐卡 限制购买门店使用
                    continue;
                }
                // $ppc=ProductPackageCity::where("city_id",$sc_user->shop->city_id)->where("status",1)->select("pack_id")->get()->toArray();
                $ppc=ProductPackageShop::getShopPackage($sc_user->shop_id);
                $scdp=ServiceCardDetailPack::leftJoin("product_package","product_package.id","=","service_card_detail_pack.pack_id")->where("service_card_detail_pack.service_card_detail_id",$_scd->id)->where("service_card_detail_pack.curr_number",">",0)
                    ->whereIn("product_package.id",$ppc)->select("service_card_detail_pack.curr_number","product_package.id","product_package.name")->get();
                foreach ($scdp as $_scdp) {
                    if(isset($finally_arr[$_scdp->id])){
                        $t_scdp=$finally_arr[$_scdp->id];
                        $t_scdp->curr_number=$t_scdp->curr_number+$_scdp->curr_number;
                        $finally_arr[$_scdp->id]=$t_scdp;
                    }else{
                        $finally_arr[$_scdp->id]=$_scdp;
                    }
                }
            }
            $temp_arr=array("count"=>sizeof($finally_arr),"service_package"=>array_values($finally_arr));
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $temp_arr);
    	}while(false);
    	Log::info("info", array("system" => "scs" ,"event" => "serviceCardList", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

  /**
   * @Author:      lyt
   * @DateTime:  2017-08-02 09:44:25
   * @Description: 用于添加一个套餐卡中的服务
   */
    public function addServiceCard(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'id'       => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if(!in_array($orders->status,Orders::$add_product)){//订单状态是正确
                $response = Lang::get("scs_error.order_status_error_40022");
                break;
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.car_model_is_empty23000");
                break;
            }
            /**
             * 选择合适的套餐卡产品 
             */
            $scdp=ServiceCardDetailPack::leftJoin("service_card_detail","service_card_detail.id","=","service_card_detail_pack.service_card_detail_id")->where("service_card_detail.user_id",$orders->user_id)->where('service_card_detail.status',2)->where("service_card_detail.end_date",">=",date("Y-m-d",time()))->where("service_card_detail_pack.pack_id",Input::get("id"))->where("service_card_detail_pack.curr_number",">",0)->orderBy("service_card_detail.end_date","asc")->select("service_card_detail_pack.*","service_card_detail.shop_id","service_card_detail.used_car_no")->get();
            $finally_arr=array();
            $msg="";
            foreach ($scdp as $_scdp) {
                $msg="成功,";
                $scb=ServiceCardBase::find($_scdp->service_card_base_id);
                $scbs=ServiceCardBaseShopUse::where("service_card_base_id",$scb->id)->where("shop_id",$sc_user->shop_id)->where("status",1)->first();//判断是否支持该门店
                if(is_null($scbs)||$scb->user_status==0){//改套餐卡不支持该门店 或者 该类型套餐卡停用
                    continue;
                }
                if($scb->limit_shop==1&&$_scdp->shop_id!=0&&$_scdp->shop_id!=$sc_user->shop_id){//说明套餐卡 限制购买门店使用
                    continue;
                }                
                if($scb->limit_car==1&&$_scdp->used_car_no!=$orders->car_no){//说明套餐卡 限制购买车辆使用
                    continue;
                }
                if($scb->limit_car==2){//说明套餐卡 限制购买用户车辆使用
                    $scc=DB::table("service_card_car")->where("user_id",$orders->user_id)->where("car_no",$orders->car_no)->where("status",1)->first();
                    if(is_null($scc)){//说明是 有绑定车辆
                        continue;
                    }
                }
                $pc=PricingColumn::all();        
                $temp_pc=array();
                foreach ($pc as $_pc) {
                    $temp_pc[$_pc->id]=$_pc;
                }
                //检测 车型参数
                $limit_array=array();
                $scbcl=ServiceCardBaseCarlimit::where("service_card_base_id",$scb->id)->select("pric_clo_id","column_id")->get();  

                foreach ($scbcl as $_scbcl) {
                    if(isset($limit_array[$_scbcl->column_id])){                        
                        $limit_array[$_scbcl->column_id]["item"][]=$_scbcl->pric_clo_id;
                    }else{
                        $temp=$temp_pc[$_scbcl->column_id];
                        $limit_array[$_scbcl->column_id]=array("column"=>$temp->column,"name"=>$temp->name,"item"=>array($_scbcl->pric_clo_id));
                    }
                }
                // $csi=DataCarStandardInfo::find($car_model_id);
                $arr=$csi->toArray();
                $falg=true;                
                foreach ($limit_array as $_la) {//验证 是否符合要求
                    $v=$arr[$_la["column"]];//获取订单车型的 车型限制字段的值
                    if(!in_array($v,$_la["item"])){//查看该车型限制字段是否是 限定值 如果不是限定 说明这个包不推荐使用
                        $falg=false;
                        $msg.=$_la["name"].",";
                    }
                }
                if($falg){//如果该条套餐卡产品的类型完全符合要求 则用该套餐卡的包 
                    $finally_arr[0]=array("status"=>0,"pack_id"=>$_scdp->pack_id,"id"=>$_scdp->id);
                    break;
                }else{//说明不推荐使用 则按照顺序 继续检查下个套餐包
                    $finally_arr[]=array("status"=>1,"pack_id"=>$_scdp->pack_id,"id"=>$_scdp->id);
                }
            }
            if(sizeof($finally_arr)==0){//说明这个包都不满足这个订单
                $response = Lang::get("scs_error.order_service_card_error_40133");
                break;
            }
             /**
             * 选择合适的套餐卡产品 结束
             * 开始添加订单
             */
            $package=ProductPackage::find($finally_arr[0]["pack_id"]);
            $p_item=ProductPackage::getItem($orders->car_model_id,$finally_arr[0]["pack_id"]);
            $whp=WorkHoursPrice::where("city_id",$sc_user->shop->city_id)->first();
            if(is_null($whp)){
                $response = Lang::get("scs_error.work_hour_error_10001");
                break;
            }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }
            $count_amount=0;
            $supply_arr=$p_item['supply'];
            $order_product_item=array();
            foreach ($supply_arr as $_sa) {
                //更换耗材的加价系数
                $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$_sa["brand_id"])->where("supply_subject_id",$_sa["subject"])->where("status",1)->first();
                $wmcf->supply_coefficient = null;
                if (!empty($wpc)) {
                    $wpc_city=WorkPartsCoefficientCity::where('city_id',$sc_user->shop->city_id)->where('coefficient_id',$wpc->id)->where('status',1)->first();
                    $wmcf->supply_coefficient=isset($wpc_city->coefficient) ? $wpc_city->coefficient : null; //如果有城市系数，优先使用。
                }
                if ($wmcf->supply_coefficient == null){ //如果是0，说明没有赋值
                    $wmcf->supply_coefficient=isset($wpc->coefficient) ? $wpc->coefficient : 1;
                }
                //更换多城市价格
                // $sip=SupplyItemPrice::where("item_id",$_sa["id"])->where("city_id",$sc_user->shop->city_id)->first();
                // if($sip){
                //     $_sa["amount"]=$sip->price;
                // }

                $sip=SupplyCityPrice::where("supply_id",$_sa["id"])->where("city_id",$sc_user->shop->city_id)->where("status",1)->first();
                if($sip){
                    $_sa["amount"]=$sip->price;
                }else{
                    $ss_temp=SupplySub::where("supply_id",$_sa["id"])->where("status",1)->first();
                    $_sa["amount"]=isset($ss_temp)?$ss_temp->usual_price:0;
                }
                $temp_amount=$_sa["amount"]*$wmcf->supply_coefficient;
                $temp_amount=Common::upFow($temp_amount);
                $order_product_item[]=array(
                    "item_id"=>$_sa["id"],
                    "item_name"=>$_sa["name"],
                    "amount"=>$temp_amount,
                    "num"=>$_sa["number"],
                    "type"=>1,
                    "unit"=>$_sa["unit"],
                    "is_adapter"=>$_sa["is_adapter"],
                );
                $count_amount+=$temp_amount*$_sa["number"];
            }
            foreach ($p_item['server'] as $_se) {
                $res=PricingStrategy::getStringStrategy($csi->id,$_se['id'],$sc_user->shop->city_id);
                if($res['code']==0){
                    $temp_amount=$res['price'];
                }else{
                    //判断工时类型为工时类型还是固定价格
                    if($_se['type'] ==1 ){
                        $temp_amount=$whp->price*$wmcf->work_coefficient*$_se["work_hours"];
                    }elseif($_se['type']==2){
                        $temp_amount=$_se["price"];
                    }
                }
                $order_product_item[]=array(
                    "item_id"=>$_se["id"],
                    "item_name"=>$_se["name"],
                    "amount"=>$temp_amount,
                    "num"=>$_se["work_hours"],
                    "type"=>2,
                    "unit"=>"个",
                    "is_adapter"=>1,
                );
                $count_amount+=$temp_amount;
            }
            $package_arr=array(
                "product_id"=>$package->id,
                "product_name"=>$package->name,
                "amount"=>$count_amount,
                "item"=>$order_product_item
            );
            OrderProduct::saveServiceCardProducts($package_arr,$sc_user->id,$orders->order_id,$finally_arr[0]["id"]);
            $response = array('code' => 0, 'msg' => $msg."不符合限制条件", 'result' => array("warn"=>$finally_arr[0]["status"],"order_id"=>Input::get('order_id')));
        }while(false);
        return json_encode($response);
    }


    

    /**
     * @Author:      lyt
     * @DateTime:  2017-08-02 15:14:39
     * @Description: 更改套餐卡包内产品数量
     */
    public function changeServiceCardNum(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_item_id'=> 'required',
                'number'=> 'required|numeric|min:0|not_in:0',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->order_type==1){
                if(!in_array($orders->status,Orders::$add_product)){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }else{
                if($orders->status!=1411){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            }
            $opci=OrderProductScItem::find(Input::get("order_product_item_id"));      
            if(is_null($opci)||$opci->order_id!=$orders->order_id||$opci->type!=1){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $order_product=OrderProduct::find($opci->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $number=Input::get("number");
            $amount=Input::get("amount")*100;
            if($number==$opci->num&&$amount==$opci->amount){
                $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_product->order_id,"order_product_amount"=>$order_product->amount,"order_amount"=>$orders->amount));
                break;
            }
            $response=OrderProductScItem::changeProductItemNum($orders->order_id,$opci,$number,$amount,$sc_user->id);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeServiceCardNum", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
    * @Author:      lyt
    * @DateTime:  2017-08-02 16:12:00
    * @Description: 用于更换套餐卡包内产品耗材
    */
    public function changeServiceCardItem(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_item_id'=> 'required',//订单耗材的id
                'new_item_id'=> 'required',
                // 'type' => 'required'//0为标准耗材1为非标
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->order_type==1){
                if(!in_array($orders->status,Orders::$add_product)){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }else{
                if($orders->status!=1411){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            }
            $opci=OrderProductScItem::find(Input::get("order_product_item_id"));      
            if(is_null($opci)||$opci->order_id!=$orders->order_id||$opci->type!=1){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $order_product=OrderProduct::find($opci->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.order_car_model_notexit_40002");
                break;
            }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }
            $si=SupplyItem::find(Input::get("new_item_id"));
            // if(is_null($si)||$si->status!=1){//如果 耗材不存在或者 状态不正确
            //     return Lang::get("scs_error.supply_item_not_exit_41000");
            // }
            //要更换成的配件类型判断
            if($si->type==0){
                $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->first();
                if(!$mw || $opci->num>$mw->number){
                    $response = Lang::get("scs_error.supply_item_low_stocks_40005");
                    break;
                }
                // $si=SupplyItem::find(Input::get("new_item_id"));
                $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$si->brand_id)->where("supply_subject_id",$si->supply_classification_id)->where("status",1)->first();
                $wmcf->supply_coefficient = null;
                if (!empty($wpc)) {
                    $wpc_city=WorkPartsCoefficientCity::where('city_id',$sc_user->shop->city_id)->where('coefficient_id',$wpc->id)->where('status',1)->first();
                    $wmcf->supply_coefficient=isset($wpc_city->coefficient) ? $wpc_city->coefficient : null; //如果有城市系数，优先使用。
                }
                if ($wmcf->supply_coefficient == null){ //如果是0，说明没有赋值
                    $wmcf->supply_coefficient=isset($wpc->coefficient) ? $wpc->coefficient : 1;
                }              
                //更换多城市价格
                // $sip=SupplyItemPrice::where("item_id",$si->id)->where("city_id",$orders->city)->first();
                // if($sip){
                //     $si->amount=$sip->price;
                // }
                $sip=SupplyCityPrice::where("supply_id",$si->id)->where("city_id",$sc_user->shop->city_id)->where("status",1)->first();
                if($sip){
                    $si->amount=$sip->price;
                }else{
                    $ss_temp=SupplySub::where("supply_id",$si->id)->where("status",1)->first();
                    $si->amount=isset($ss_temp)?$ss_temp->usual_price:0;
                }        
                $si->amount=Common::upFow($si->amount*$wmcf->supply_coefficient);                
            }else{
                $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->first();
                if(!$mw || $opci->num>$mw->number){
                    $response = Lang::get("scs_error.supply_item_low_stocks_40005");
                    break;
                }
                // $si=SupplyItemNonStandard::find(Input::get("new_item_id"));
                // if(Orders::isSale($si->id,$si->type)){
                //     $response = Lang::get("scs_error.supply_error_10011");
                //     break;
                // }
                $si->amount=Input::get("amount")*100;//如果是非标的 更新非标为传过来的价格
            }
            if(Orders::isSale($si->id,$si->type)){
                $response = Lang::get("scs_error.supply_error_10011");
                break;
            }
            //原有配件类型判断
            // if($opci->item_type==0){
                $old_si=SupplyItem::find($opci->item_id);
            // }else{
            //     $old_si=SupplyItemNonStandard::find($opci->item_id);
            // }

            //如果更换非标不进行 耗材品类判断
            if($si->type==0){
                $temp_subject=($opci->temp_subject==0)?$old_si->supply_classification_id:$opci->temp_subject;//如果
                if($temp_subject!=$si->supply_classification_id){
                    $response = Lang::get("scs_error.supply_subject_error_41002");
                    break;
                }
            }
            if($opci->temp_subject==0){
                $opci->temp_subject=$old_si->supply_classification_id;
            }
            $response=OrderProductScItem::changeProductItem($orders->order_id,$opci,$si,$sc_user->id,$wmcf,$si->type);

        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeBaseProductItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:  2017-04-11 15:22:46
     * @Description:修改用户的 
     */
    public function changeServiceCardItemBefore(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_item_id'=> 'required',//订单耗材的id
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->order_type==1){
                if(!in_array($orders->status,Orders::$add_product)){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }else{
                if($orders->status!=1411){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            }
            $opci=OrderProductScItem::find(Input::get("order_product_item_id"));      
            if(is_null($opci)||$opci->order_id!=$orders->order_id||$opci->type!=1){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $order_product=OrderProduct::find($opci->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.order_car_model_notexit_40002");
                break;
            }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }    
            $moo=MaintenanceOutOrder::where("product_type",$order_product->product_type)->where("product_item_id",$opci->id)->whereIn("status",array(0,1,2))->first();
            if($moo){//如果存在 备货中或者 等待领料的 出库申请 已经领料的 不允许修改
                $response = Lang::get("scs_error.order_product_item_change_error_40012");
                break;
            }
            
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeServiceCardItemBefore", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      lyt
     * @DateTime:  2017-08-02 18:17:13
     * @Description: 用于套餐卡产品的 不适配耗材更换
     */
    public function changeNoAdapterServiceCardItem(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_item_id'=> 'required',//订单耗材的id
                'new_item_id'=> 'required',
                // 'type' => 'required'//0为标准耗材1为非标
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->order_type==1){
                if(!in_array($orders->status,Orders::$add_product)){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }else{
                if($orders->status!=1411){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            }
            $opci=OrderProductScItem::find(Input::get("order_product_item_id"));      
            if(is_null($opci)||$opci->order_id!=$orders->order_id||$opci->type!=1){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $order_product=OrderProduct::find($opci->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.order_car_model_notexit_40002");
                break;
            }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }    
            $si=SupplyItem::find(Input::get("new_item_id"));
            // if(is_null($si)||$si->status!=1){//如果 耗材不存在或者 状态不正确
            //     return Lang::get("scs_error.supply_item_not_exit_41000");
            // }
            //要更换成的配件类型判断
            if($si->type==0){
                $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',$si->type)->first();
                if(!$mw || $opci->num>$mw->number){
                    $response = Lang::get("scs_error.supply_item_low_stocks_40005");
                    break;
                }
                // $si=SupplyItem::find(Input::get("new_item_id"));
                $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$si->brand_id)->where("supply_subject_id",$si->supply_classification_id)->where("status",1)->first();
                $wmcf->supply_coefficient = null;
                if (!empty($wpc)) {
                    $wpc_city=WorkPartsCoefficientCity::where('city_id',$sc_user->shop->city_id)->where('coefficient_id',$wpc->id)->where('status',1)->first();
                    $wmcf->supply_coefficient=isset($wpc_city->coefficient) ? $wpc_city->coefficient : null; //如果有城市系数，优先使用。
                }
                if ($wmcf->supply_coefficient == null){ //如果是0，说明没有赋值
                    $wmcf->supply_coefficient=isset($wpc->coefficient) ? $wpc->coefficient : 1;
                }
                //更换多城市价格
                // $sip=SupplyItemPrice::where("item_id",$si->id)->where("city_id",$orders->city)->first();
                // if($sip){
                //     $si->amount=$sip->price;
                // }
                $sip=SupplyCityPrice::where("supply_id",$si->id)->where("city_id",$sc_user->shop->city_id)->where("status",1)->first();
                if($sip){
                    $si->amount=$sip->price;
                }else{
                    $ss_temp=SupplySub::where("supply_id",$si->id)->where("status",1)->first();
                    $si->amount=isset($ss_temp)?$ss_temp->usual_price:0;
                }        
                $si->amount=Common::upFow($si->amount*$wmcf->supply_coefficient);    
            }else{
                $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->first();
                if(!$mw || $opci->num>$mw->number){
                    $response = Lang::get("scs_error.supply_item_low_stocks_40005");
                    break;
                }
                // $si=SupplyItemNonStandard::find(Input::get("new_item_id"));
                // if(Orders::isSale($si->id,1)){
                //     $response = Lang::get("scs_error.supply_error_10011");
                //     break;
                // }
                $si->amount=Input::get("amount")*100;//如果是非标的 更新非标为传过来的价格
            }

            if(Orders::isSale($si->id,$si->type)){
                $response = Lang::get("scs_error.supply_error_10011");
                break;
            }
            //如果更换非标不进行 耗材品类判断
            if($si->type==0){
                if($opci->item_id!=$si->supply_classification_id){//在添加无适配耗材时耗材id存入的是品类id
                    $response = Lang::get("scs_error.supply_subject_error_41002");
                    break;
                }
            }
            $opci->temp_subject=$opci->item_id;
            $response=OrderProductScItem::changeProductItem($orders->order_id,$opci,$si,$sc_user->id,$wmcf,$si->type);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeNoAdapterServiceCardItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}